package uk.ac.cam.bt288.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DialogActivity extends Activity {
	CharSequence[] items = {"Google", "Apple", "Microsoft"};
	boolean[] itemsChecked = new boolean[items.length];
	
	private ProgressDialog _progressDialog;
	private int _progress = 0;
	private Handler _progressHandler;
	
	//request data from another activity
	int request_Code = 1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button) findViewById(R.id.btn_dialog);
        
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(1);
				
				_progress = 0;
				_progressDialog.setProgress(0);
				_progressHandler.sendEmptyMessage(0);
				
			}
		});
        _progressHandler = new Handler() {
        	public void handleMessage(Message msg) {
        		super.handleMessage(msg);
        		if (_progress >= 150)
        			_progressDialog.dismiss();
        		else {
        			_progress++;
        			_progressDialog.incrementProgressBy(1);
        			_progressHandler.sendEmptyMessageDelayed(0, 20);
        		}
        	}
        };
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event){
    	if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
    		//startActivity(new Intent("uk.ac.cam.bt288.Activity2"));
    		//startActivityForResult(new Intent("uk.ac.cam.bt288.Activity2"), request_Code);
    		Intent i = new Intent("uk.ac.cam.bt288.Activity2");
    		Bundle extras = new Bundle();
    		extras.putString("Name", "Your name here");
    		i.putExtras(extras);
    		startActivityForResult(i,1);
    	}
    	
    	return false;
    }
    
    public void onActivityResult (int requestCode, int resultCode, Intent data) {
    	if (requestCode == request_Code)
    		if (resultCode == RESULT_OK)
    			Toast .makeText (this, data.getData().toString(), Toast.LENGTH_SHORT).show();
    }
    
    protected Dialog onCreateDialog (int id) {
    	switch (id) {
    	case 0:
    		return new AlertDialog.Builder(this)
    		.setIcon(R.drawable.ic_launcher)
    		.setTitle("This is a dialog with simple text...")
    		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getBaseContext(), "Ok!", Toast.LENGTH_SHORT).show();
					
				}
			})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getBaseContext(), "Cancel", Toast.LENGTH_SHORT).show();					
				}
			})
			
			.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast .makeText(getBaseContext(), items[which]+(isChecked?" Checked!":" unchecked!"), Toast.LENGTH_SHORT).show();					
				}
			})
			.create();
    	case 1:
    		_progressDialog = new ProgressDialog(this);
    		_progressDialog.setIcon(R.drawable.ic_launcher);
    		_progressDialog.setTitle("Loading files...");
    		
    		_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    		_progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast .makeText(getBaseContext(), "Hide clicked!", Toast.LENGTH_SHORT).show();
					
				}
			});
    		_progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast .makeText(getBaseContext(), "Cancel", Toast.LENGTH_SHORT).show();
					
				}
			});
    		return _progressDialog;
    	}
    	return null;
    }
}